package screens;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import runnables.DownloadRunnable;
import strings.WebcampusResource;
import util.LookAndFeel;
import beans.WebCampusFile;
import customfields.ButtonUADE;

public class FileDownloadScreen extends TransitionScreen implements WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private WebCampusFile wcfile;

	public FileDownloadScreen(WebCampusFile file, int transitionType) {
		super(transitionType);

		this.wcfile=file;

		// Main vertical field manager
		VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_HEIGHT);
		Bitmap background_bitmap = Bitmap.getBitmapResource(LookAndFeel.background_img);
		Bitmap background_bitmap_resized = new Bitmap(Display.getWidth(), background_bitmap.getHeight());
		background_bitmap.scaleInto(background_bitmap_resized, Bitmap.FILTER_LANCZOS | Bitmap.SCALE_TO_FILL);
		Background background = BackgroundFactory.createBitmapBackground(background_bitmap_resized);
		vfm.setBackground(background);

		// Header
		Bitmap header_img = Bitmap.getBitmapResource(LookAndFeel.header_img);
		Bitmap header_img_resized = new Bitmap(Display.getWidth(), (int) (Display.getHeight()*0.25));
		header_img.scaleInto(header_img_resized, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
		BitmapField header = new BitmapField(header_img_resized,Field.FIELD_HCENTER);
		vfm.add(header);

		// Separator
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		// File data

		vfm.add(new LabelField(file.getName(), Field.FIELD_HCENTER));
		vfm.add(new LabelField(resources.getString(FILE_DATE)+file.getDate(), Field.FIELD_HCENTER));
		vfm.add(new LabelField(resources.getString(FILE_EXTENSION)+file.getExtension(), Field.FIELD_HCENTER));
		vfm.add(new LabelField(resources.getString(FILE_SIZE)+String.valueOf(file.getSize()/1024)+" KB", Field.FIELD_HCENTER));
		vfm.add(new LabelField(""));

		// DOWNLOAD BUTTON
		ButtonUADE download = new ButtonUADE(resources.getString(DOWNLOAD_LABEL), Field.FIELD_HCENTER);
		download.setChangeListener(downloadListener);
		vfm.add(download);

		add(vfm);

	}
	private FieldChangeListener downloadListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			Controller.getInstance().setUrl(wcfile.getLink());
			Controller.getInstance().setFilename(wcfile.getName()+wcfile.getExtension());
			Controller.getInstance().setSize(wcfile.getSize());
			DownloadingPopupScreen.showScreenAndWait(new DownloadRunnable(), resources.getString(POPUP_DOWNLOAD));
		}
	};

	// dialogs
	public static void showError(String message){
		Dialog.alert(message);
	}
	
}
